//
//  NSObject+ZBCalendarTool.m
//  ZuoBiao
//
//  Created by stoneobs on 17/3/22.
//  Copyright © 2017年 shixinyun. All rights reserved.
//

#import "NSCalendar+STTools_CalendarTool.h"
@implementation NSCalendar (STTools_CalendarTool)
+ (NSString *)stoneobs_fun_yearAndMonthByDate:(NSDate *)date{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *stoneobs_temp_comps = [stoneobs_temp_greCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:date];
    return [NSString stringWithFormat:@"%ld年%ld月",stoneobs_temp_comps.year,stoneobs_temp_comps.month];
}

+ (NSInteger)stoneobs_fun_indexOfTheMonthDate:(NSDate *)date
{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *stoneobs_temp_comps = [stoneobs_temp_greCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:date];
    return stoneobs_temp_comps.day;
}

+ (NSInteger)stoneobs_fun_whatTheMonthBeginInWeekByDate:(NSDate *)date
{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *stoneobs_temp_comps = [stoneobs_temp_greCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday|NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:date];
    stoneobs_temp_comps.day = 1;
    NSDate * monthBginDate = [stoneobs_temp_greCalendar dateFromComponents:stoneobs_temp_comps];
    //计算这天周几
    NSCalendar *dayCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [dayCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];//Asia/Shanghai
    NSDateComponents *daystoneobs_temp_comps = [dayCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth  | NSCalendarUnitWeekday| NSCalendarUnitDay|NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:monthBginDate];
    return daystoneobs_temp_comps.weekday;
}
+ (NSInteger)stoneobs_fun_whatTheCurrentInWeekByDate:(NSDate*)date{
    NSDateComponents *astoneobs_temp_comps = [self stoneobs_fun_comWithDate:date];
    return astoneobs_temp_comps.weekday;
}
+ (NSInteger)stoneobs_fun_numOfMonthDaysByDate:(NSDate *)date{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    return [stoneobs_temp_greCalendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;
}

+ (NSDate *)stoneobs_fun_lastMonthDate:(NSDate *)date
{
    NSCalendar *stoneobs_temp_greCalendar = [NSCalendar currentCalendar];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
     [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone systemTimeZone]];
    NSDateComponents *stoneobs_temp_comps = [stoneobs_temp_greCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:date];
    stoneobs_temp_comps.month =stoneobs_temp_comps.month - 1;
    stoneobs_temp_comps.day = 15; //比如 3月31 如果获取到上一个月，按照算法就是 2月31 日，当然不可能存在，自动换算成3月3日，所以错误，手动到一个月的15号，保证结果是上一个月
    NSDate * lastDate =[stoneobs_temp_greCalendar dateFromComponents:stoneobs_temp_comps];
    return lastDate;
}
+ (NSDate *)stoneobs_fun_nextMonthDate:(NSDate *)date
{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *stoneobs_temp_comps = [stoneobs_temp_greCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:date];
    stoneobs_temp_comps.month += 1;
    stoneobs_temp_comps.day = 15;//比如 1月31 如果获取到下一个月，按照算法就是 2月31 日，当然不可能存在，自动换算成3月3日，所以错误，手动到一个月的15号，保证结果是下一个月
    return [stoneobs_temp_greCalendar dateFromComponents:stoneobs_temp_comps];
}
+ (NSDate *)stoneobs_fun_lastDayDate:(NSDate *)date
{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *stoneobs_temp_comps = [stoneobs_temp_greCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:date];
    stoneobs_temp_comps.day =stoneobs_temp_comps.day - 1;
    return [stoneobs_temp_greCalendar dateFromComponents:stoneobs_temp_comps];
}
//上多少天
+ (NSDate *)stoneobs_fun_lastDayDate:(NSDate *)date day:(NSInteger)day{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *stoneobs_temp_comps = [stoneobs_temp_greCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:date];
    stoneobs_temp_comps.day =stoneobs_temp_comps.day - day;
    return [stoneobs_temp_greCalendar dateFromComponents:stoneobs_temp_comps];
}
+ (NSDate *)stoneobs_fun_nextDayDate:(NSDate *)date
{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *stoneobs_temp_comps = [stoneobs_temp_greCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:date];
    stoneobs_temp_comps.day =stoneobs_temp_comps.day + 1;
    return [stoneobs_temp_greCalendar dateFromComponents:stoneobs_temp_comps];
}
//下多少天
+ (NSDate *)stoneobs_fun_nextDayDate:(NSDate *)date day:(NSInteger)day{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *stoneobs_temp_comps = [stoneobs_temp_greCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:date];
    stoneobs_temp_comps.day =stoneobs_temp_comps.day + day;
    return [stoneobs_temp_greCalendar dateFromComponents:stoneobs_temp_comps];
}
+ (NSDateComponents *)stoneobs_fun_comWithDate:(NSDate *)date
{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *stoneobs_temp_comps = [stoneobs_temp_greCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday| NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:date];
    
    return stoneobs_temp_comps;
}
+ (NSDate *)stoneobs_fun_dateFromCom:(NSDateComponents *)com{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    return [stoneobs_temp_greCalendar dateFromComponents:com];
}
+ (NSDate *)stoneobs_fun_dateForMonthFirstDay:(NSDate *)date
{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *stoneobs_temp_comps = [stoneobs_temp_greCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday| NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:date];
    stoneobs_temp_comps.day = 1;
    stoneobs_temp_comps.hour = 0;
    stoneobs_temp_comps.minute = 0;
    stoneobs_temp_comps.second = 1;
    return [stoneobs_temp_greCalendar dateFromComponents:stoneobs_temp_comps];
}
+ (NSDate *)stoneobs_fun_dateForWeekFirstDay:(NSDate *)date{
    NSCalendar *dayCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [dayCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];//Asia/Shanghai
    NSDateComponents *daystoneobs_temp_comps = [dayCalendar
                                  components:NSCalendarUnitYear | NSCalendarUnitMonth  | NSCalendarUnitWeekday| NSCalendarUnitDay|NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                                  fromDate:date];
    NSInteger index = daystoneobs_temp_comps.weekday;
    index = (index - 2)>=0?(index - 2):(index - 2)+7;
    daystoneobs_temp_comps.day = daystoneobs_temp_comps.day - index;
    return [dayCalendar dateFromComponents:daystoneobs_temp_comps];
}
+ (NSDate *)stoneobs_fun_dateForWeekLastDay:(NSDate *)date{
    NSCalendar *dayCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [dayCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];//Asia/Shanghai
    NSDateComponents *daystoneobs_temp_comps = [dayCalendar
                                  components:NSCalendarUnitYear | NSCalendarUnitMonth  | NSCalendarUnitWeekday| NSCalendarUnitDay|NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                                  fromDate:date];
    NSInteger index = daystoneobs_temp_comps.weekday;
    index = (- index + 1)<0?(7 - index + 1):0;
    daystoneobs_temp_comps.day = daystoneobs_temp_comps.day + index;
    return [dayCalendar dateFromComponents:daystoneobs_temp_comps];
}
//获取这个月 某天的日期
+ (NSDate*)stoneobs_fun_dateFromMonthDate:(NSDate*)monthDate day:(NSInteger)day{
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *stoneobs_temp_comps = [stoneobs_temp_greCalendar
                               components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday| NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond
                               fromDate:monthDate];
    stoneobs_temp_comps.day = day;
    return [stoneobs_temp_greCalendar dateFromComponents:stoneobs_temp_comps];
}
@end
